package com.obas.access.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ease.common.util.Assertion;
import com.ease.common.vo.MapParameter;
import com.ease.platform.access.service.impl.AbstractParameterizedBeanIbatisAccessServiceImpl;
import com.obas.access.business.service.CircuitAccessService;
import com.obas.model.business.Circuit;
import com.obas.model.metadata.constant.EntityTypeConst;

public class CircuitAccessServiceImpl extends AbstractParameterizedBeanIbatisAccessServiceImpl<Circuit> implements CircuitAccessService {

	class Statement {
		public static final String findRecuisiveByUpperDeviceId = "findRecuisiveByUpperDeviceId";
		public static final String findRecuisiveByLowerDeviceId = "findRecuisiveByLowerDeviceId";
		public static final String findByAendDeviceIdZendDeviceId = "findByAendDeviceIdZendDeviceId";
	}

	public List<Circuit> findLinkRecuisiveByUpperDeviceId(long upperDeviceId) {
		Assertion.isPositive(upperDeviceId, "upperDeviceId is not positive, upperDeviceId=" +upperDeviceId);
		MapParameter parameter = new MapParameter();
		parameter.put(Circuit.ATTRIBUTE_entityTypeId, EntityTypeConst.LINK.getId());
		parameter.put("deviceId", upperDeviceId);
		return this.find(Statement.findRecuisiveByUpperDeviceId, parameter);

	}

	public List<Circuit> findLinkRecuisiveByLowerDeviceId(long lowerDeviceId) {
		Assertion.isPositive(lowerDeviceId, "lowerDeviceId is not positive, lowerDeviceId=" +lowerDeviceId);
		MapParameter parameter = new MapParameter();
		parameter.put(Circuit.ATTRIBUTE_entityTypeId, EntityTypeConst.LINK.getId());
		parameter.put("deviceId", lowerDeviceId);
		return this.find(Statement.findRecuisiveByLowerDeviceId, parameter);
	}

	public List<Circuit> findByAendDeviceIdZendDeviceId(long aendDeviceId, long zendDeviceId){
		Assertion.isPositive(aendDeviceId, "aendDeviceId is not positive, aendDeviceId=" +aendDeviceId);
		Assertion.isPositive(zendDeviceId, "zendDeviceId is not positive, zendDeviceId=" +zendDeviceId);
		MapParameter parameter = new MapParameter();
		parameter.put("aendDeviceId", aendDeviceId);
		parameter.put("zendDeviceId", zendDeviceId);
		return this.find(Statement.findByAendDeviceIdZendDeviceId, parameter);
	}

	public List<Circuit> findByAendDeviceIdZendDeviceIds(long aendDeviceId, long[] zendDeviceIds) {
		MapParameter parameter = new MapParameter();
		parameter.put("aendDeviceId", aendDeviceId);
		parameter.put("zendDeviceIds", zendDeviceIds);
		return this.find(parameter);
	}

	public List<Circuit> findByZendDeviceIdAendDeviceIds(long zendDeviceId, long[] aendDeviceIds) {
		MapParameter parameter = new MapParameter();
		parameter.put("zendDeviceId", zendDeviceId);
		parameter.put("aendDeviceIds", aendDeviceIds);
		return this.find(parameter);
	}

	public List<Circuit> findByDeviceIds(long[] deviceIds) {
		if (notEmpty(deviceIds)) {
			Map<Long, Circuit> circuits = new HashMap<Long, Circuit>();
			List<Circuit> temps = null;
			for (long deviceId : deviceIds) {
				temps = findByAendDeviceIdZendDeviceIds(deviceId, deviceIds);
				if (notEmpty(temps)) {
					for (Circuit circuit : temps) {
						circuits.put(circuit.getId(), circuit);
					}
				}
				temps = findByAendDeviceIdZendDeviceIds(deviceId, deviceIds);
				if (notEmpty(temps)) {
					for (Circuit circuit : temps) {
						circuits.put(circuit.getId(), circuit);
					}
				}
			}
			return escapeEmpty(new ArrayList<Circuit>(circuits.values()));
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.obas.access.business.service.CircuitAccessService#findUpperRouteByUserId(long)
	 */
	@Override
	public Circuit findUpperRouteByUserId(long customerId) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.obas.access.business.service.CircuitAccessService#findLowerRouteByUserId(long)
	 */
	@Override
	public List<Circuit> findLowerRouteByUserId(long customerId) {
		// TODO Auto-generated method stub
		return null;
	}

}
